﻿#include "formdefaultstyle.h"
#include "ui_formdefaultstyle.h"
#include <QMenu.h>
#include "dlgadvancesetting.h"
#include "qwininterface.h"
#include <QMessageBox.h>
#include "Device.h"
#include <QDebug>
#include <QHttpMultiPart>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QMetaObject>
#include <QEventLoop>
#include <QJsonDocument>            //以下是json数据传送所需头文件
#include <QJsonParseError>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonArray>


FormDefaultStyle::FormDefaultStyle(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormDefaultStyle),
    pop_menu(NULL)
{
    ui->setupUi(this);
    //GetDevices(0);
    setStyleSheet("QPushButton{font:bold 12px \"Microsoft YaHei\";color: rgb(120, 120, 120);}"
                  "QPushButton::hover{font:bold 12px \"Microsoft YaHei\";color: rgb(80, 80, 80);border: 1px solid gray;}"
                  "QPushButton::pressed{font:bold 12px \"Microsoft YaHei\";color: rgb(80, 80, 80);border: 1px solid gray;}"
                  "QPushButton::checked{font:bold 12px \"Microsoft YaHei\";color: rgb(80, 80, 80);border: 1px solid green;}"
                  "QLabel{font:bold 14px \"Microsoft YaHei\";color: rgb(120, 120, 120);}"
                  "QCheckBox{font:bold 12px \"Microsoft YaHei\";color: rgb(120, 120, 120);}");
    init_controls();
   
}

FormDefaultStyle::~FormDefaultStyle()
{
    delete pop_menu;
    delete ui;
}

void FormDefaultStyle::init_controls()
{
    ui->pushButton_start_make->setStyleSheet("QPushButton{border-image: url(:/images/1_1_Btn1_1.jpg);}"
                                             "QPushButton:hover{border-image: url(:/images/1_1_Btn1_2.jpg);}"
                                             "QPushButton:pressed{border-image: url(:/images/1_1_Btn1_3.jpg);}");
    ui->pushButton_more->setStyleSheet("QPushButton{border-image:url(:/images/1_more_1.jpg);}"
                                       "QPushButton:hover{border-image:url(:/images/1_more_2.jpg);}"
                                       "QPushButton:pressed{border-image: url(:/images/1_more_3.jpg);}");
    //创建菜单、菜单项
    pop_menu = new QMenu();
    QAction *add_images_action = new QAction(this);
    QAction *add_folder_action = new QAction(this);
    QAction *remove_selected_action = new QAction(this);
    QAction *remove_all_action = new QAction(this);
    add_images_action->setIcon(QIcon(":/images/2_Btn1_1.jpg"));
    add_images_action->setText(QString::fromLocal8Bit("归还空间"));
    add_folder_action->setIcon(QIcon(":/images/2_Btn2_1.jpg"));
    add_folder_action->setText(QString::fromLocal8Bit("格式转换"));
    remove_selected_action->setIcon(QIcon(":/images/2_Btn3_1.jpg"));
    remove_selected_action->setText(QString::fromLocal8Bit("模拟启动"));
    remove_all_action->setIcon(QIcon(":/images/2_Btn4_1.jpg"));
    remove_all_action->setText(QString::fromLocal8Bit("升级启动盘"));
    //清除原有菜单
    pop_menu->addAction(add_images_action);
    pop_menu->addAction(add_folder_action);
    pop_menu->addAction(remove_selected_action);
    pop_menu->addAction(remove_all_action);
    connect(ui->comboBox_mand, SIGNAL(currentIndexChanged(const QString&)), SLOT(ComboBoxClicked(const QString&)));
    GetRequest();
    HWND windID = (HWND)this->winId();
    Device dev;
    device= &dev;
    device->initSet(windID);
    //device->getDevice();//只支持一个设备
    /*for (int i = 0; i < length; i++)
    {*/
        //Device device(rufus_drive[i]->index, rufus_drive[i]->display_name);
        //QVariant courseModelVariant = QVariant::fromValue(device);
        string index=to_string(device->getDeviceNum());
        string dispalyName = device->getDispalyName();
        ui->comboBox_select_device->insertItem(0, dispalyName.c_str(),index.c_str());
    //}
    //================end=======================//
    
    
}

void FormDefaultStyle::on_pushButton_hddfat32_clicked()
{
    if(!ui->pushButton_zipfat32->isChecked())
    {
        ui->pushButton_hddfat32->setChecked(true);
        ui->pushButton_compat->setChecked(true);
        return;
    }

    if(ui->pushButton_hddfat32->isChecked())
    {
        //ui->pushButton_hddfat16->setChecked(false);
        ui->pushButton_zipfat32->setChecked(false);
        ui->pushButton_auto->setEnabled(true);
        ui->pushButton_auto->setChecked(true);
        ui->pushButton_compat->setEnabled(false);
        ui->pushButton_compat->setChecked(false);
        //ui->pushButton_zipfat16->setChecked(false);
    }
}

void FormDefaultStyle::on_pushButton_zipfat32_clicked()
{
    if(//!ui->pushButton_hddfat16->isChecked() &&
       !ui->pushButton_hddfat32->isChecked()) //&&
       //!ui->pushButton_zipfat16->isChecked())
    {
        ui->pushButton_zipfat32->setChecked(true);
        return;
    }
    if(ui->pushButton_zipfat32->isChecked())
    {
        //ui->pushButton_hddfat16->setChecked(false);
        ui->pushButton_hddfat32->setChecked(false);
        ui->pushButton_compat->setEnabled(true);
        ui->pushButton_compat->setChecked(true);
        ui->pushButton_auto->setEnabled(false);
        ui->pushButton_auto->setChecked(false);
        //ui->pushButton_zipfat16->setChecked(false);
    }
}

void FormDefaultStyle::on_pushButton_hddfat16_clicked()
{
    if(!ui->pushButton_zipfat32->isChecked() &&
       !ui->pushButton_hddfat32->isChecked()) //&&
       //!ui->pushButton_zipfat16->isChecked())
    {
        //ui->pushButton_hddfat16->setChecked(true);
        return;
    }
    /*if(ui->pushButton_hddfat16->isChecked())
    {
        ui->pushButton_zipfat32->setChecked(false);
        ui->pushButton_hddfat32->setChecked(false);
        ui->pushButton_zipfat16->setChecked(false);
    }*/
}

void FormDefaultStyle::on_pushButton_zipfat16_clicked()
{
    if(!ui->pushButton_zipfat32->isChecked() &&
       !ui->pushButton_hddfat32->isChecked())//&&
       //!ui->pushButton_hddfat16->isChecked())
    {
        /*ui->pushButton_zipfat16->setChecked(true);*/
        return;
    }
    /*if(ui->pushButton_zipfat16->isChecked())
    {
        ui->pushButton_zipfat32->setChecked(false);
        ui->pushButton_hddfat32->setChecked(false);
        ui->pushButton_hddfat16->setChecked(false);
    }*/
}

void FormDefaultStyle::on_pushButton_auto_clicked()
{
    if(!ui->pushButton_compat->isChecked() &&
       !ui->pushButton_enhance->isChecked())
    {
        ui->pushButton_auto->setChecked(true);
        return;
    }
    if(ui->pushButton_auto->isChecked())
    {
        ui->pushButton_compat->setChecked(false);
        ui->pushButton_enhance->setChecked(false);
    }
}

void FormDefaultStyle::on_pushButton_compat_clicked()
{
    if(!ui->pushButton_auto->isChecked() &&
       !ui->pushButton_enhance->isChecked())
    {
        ui->pushButton_compat->setChecked(true);
        return;
    }
    if(ui->pushButton_compat->isChecked())
    {
        ui->pushButton_auto->setChecked(false);
        ui->pushButton_enhance->setChecked(false);
    }
}

void FormDefaultStyle::on_pushButton_enhance_clicked()
{
   /* if(!ui->pushButton_auto->isChecked() &&
       !ui->pushButton_compat->isChecked())
    {
        ui->pushButton_enhance->setChecked(true);
        return;
    }*/
    if(ui->pushButton_enhance->isChecked())
    {
        ui->pushButton_auto->setChecked(false);
        ui->pushButton_compat->setChecked(false);
    }
}

void FormDefaultStyle::on_pushButton_personal_default_clicked()
{
    /*if(!ui->pushButton_personal_advance->isChecked())
    {
        ui->pushButton_personal_default->setChecked(true);
        return;
    }*/
   /* if(ui->pushButton_personal_default->isChecked())
    {
        ui->pushButton_personal_advance->setChecked(false);
    }*/
}

extern DlgAdvanceSetting *dlgAdvanceSetting;

void FormDefaultStyle::on_pushButton_personal_advance_clicked()
{
    //ui->pushButton_personal_default->setChecked(false);
    //ui->pushButton_personal_advance->setChecked(true);
    if(nullptr == dlgAdvanceSetting)
    {
        dlgAdvanceSetting = new DlgAdvanceSetting(nullptr);
        dlgAdvanceSetting->setModal(false);
        dlgAdvanceSetting->show();
    }else{
        dlgAdvanceSetting->show();
    }
    // TODO : hide the parent until the "advance window" be closed.
    //hide();
}

void FormDefaultStyle::on_pushButton_more_clicked()
{
    //菜单出现的位置为当前鼠标的位置
    pop_menu->exec(mapToGlobal(QPoint(560,35)));
}
void FormDefaultStyle::on_pushButton_start_make_clicked()
{
    HWND windID = (HWND)this->parentWidget()->parentWidget()->winId();//获取当前this窗口的句柄
    int boot_type;
    long deviceNum;
    if (ui->pushButton_hddfat32->isChecked())
    {
        boot_type = 2;
    }
    if (ui->pushButton_zipfat32->isChecked())
    {
        boot_type = 9;
    }
    QVariant qv=ui->comboBox_select_device->currentData();
    deviceNum = qv.toInt();
    if (deviceNum < 0)
    {
        QMessageBox::information(this, QString::fromLocal8Bit("消息提醒"), QString::fromLocal8Bit("请插入一张U盘"), QMessageBox::Ok);
        return;
    }
    device->startFormat(boot_type, deviceNum,NULL,windID);
}
//get请求
void FormDefaultStyle::GetRequest()
{
    QNetworkRequest request1;
    QNetworkAccessManager nam;
    connect(&nam, &QNetworkAccessManager::finished, this, &FormDefaultStyle::receiveGaodeGetReply);

    //get与post的请求方式有所不同，get是在接口名后添加 ? 和传输的数据(type)
    request1.setUrl(QUrl("https://order.ylttgy.com/ponybios/queryPony/"));
    request1.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json;charset=utf-8"));
    QNetworkReply* reply = nam.get(request1);    //get请求头

    //开启事件循环，直到请求完成
    QEventLoop loop;
    connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    loop.exec();
}

//接收get请求返回的数据
void FormDefaultStyle::receiveGaodeGetReply(QNetworkReply* reply)
{
    QString res = reply->readAll();
    QJsonDocument m_document;
    QJsonParseError error;
    m_document = QJsonDocument::fromJson(res.toUtf8(), &error);
    QJsonArray array;
    if (m_document.isArray()) {
        array = m_document.array();
    }

    for (int i = 0; i < array.size(); i++) {
        if (array.at(i).isObject()) {
            QJsonObject object = array.at(i).toObject();
            if (object.contains("name")) {
                ui->comboBox_mand->addItem(object.value("name").toString(), object.value("id").toString());
            }
        }
    }
}

//get请求
void FormDefaultStyle::GetRequest_chips(QString mid)
{
    QNetworkRequest request1;
    QNetworkAccessManager nam;
    connect(&nam, &QNetworkAccessManager::finished, this, &FormDefaultStyle::receiveGaodeGetReply_chips);

    //get与post的请求方式有所不同，get是在接口名后添加 ? 和传输的数据(type)
    request1.setUrl(QUrl("https://order.ylttgy.com/ponybios/queryChipsAll/"+mid));
    request1.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json;charset=utf-8"));
    QNetworkReply* reply = nam.get(request1);    //get请求头
    //开启事件循环，直到请求完成
    QEventLoop loop;
    connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    loop.exec();
}

//接收get请求返回的数据
void FormDefaultStyle::receiveGaodeGetReply_chips(QNetworkReply* reply)
{
    QString res = reply->readAll();
    QJsonDocument m_document;
    QJsonParseError error;
    m_document = QJsonDocument::fromJson(res.toUtf8(), &error);
    QJsonArray array;
    if (m_document.isArray()) {
        array = m_document.array();
    }

    for (int i = 0; i < array.size(); i++) {
        if (array.at(i).isObject()) {
            QJsonObject object = array.at(i).toObject();
            if (object.contains("name")) {
                ui->comboBox_chips_model->addItem(object.value("name").toString(), object.value("id").toString());
            }
        }
    }
}

void FormDefaultStyle::ComboBoxClicked(QString item) {
    QVariant qv = ui->comboBox_mand->currentData();
    QString mid=qv.toString();
    ui->comboBox_chips_model->clear();
    GetRequest_chips(mid);
}

